package com.wuliu.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuliu.common.constant.CacheConstants;
import com.wuliu.common.core.redis.RedisCache;
import com.wuliu.common.exception.ServiceException;
import com.wuliu.common.utils.DateUtils;

import javax.annotation.Resource;

import com.wuliu.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.wuliu.system.mapper.SysAccountConfigMapper;
import com.wuliu.common.core.domain.entity.SysAccountConfig;
import com.wuliu.system.service.ISysAccountConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 配置字典Service业务层处理
 *
 * @author realue
 * @date 2023-10-28
 */
@Service
public class SysAccountConfigServiceImpl extends ServiceImpl<SysAccountConfigMapper, SysAccountConfig> implements ISysAccountConfigService {
    @Resource
    private SysAccountConfigMapper sysAccountConfigMapper;
    @Resource
    private RedisCache redisCache;

    /**
     * 查询配置字典
     *
     * @param id 配置字典主键
     * @return 配置字典
     */
    @Override
    public SysAccountConfig selectSysAccountConfigById(String id) {
        return sysAccountConfigMapper.selectSysAccountConfigById(id);
    }

    /**
     * 查询配置字典列表
     *
     * @param sysAccountConfig 配置字典
     * @return 配置字典
     */
    @Override
    public List<SysAccountConfig> selectSysAccountConfigList(SysAccountConfig sysAccountConfig) {
        return sysAccountConfigMapper.selectSysAccountConfigList(sysAccountConfig);
    }

    /**
     * 新增配置字典
     *
     * @param sysAccountConfig 配置字典
     * @return 结果
     */
    @Override
    public int insertSysAccountConfig(SysAccountConfig sysAccountConfig) {
        long count = sysAccountConfigMapper.selectCount(new LambdaQueryWrapper<SysAccountConfig>()
                .eq(SysAccountConfig::getSysAccount, sysAccountConfig.getSysAccount())
                .eq(SysAccountConfig::getKeyWord, sysAccountConfig.getKeyWord()));
        if (count > 0) {
            throw new ServiceException("关键字重复");
        }
        sysAccountConfig.setCreateTime(DateUtils.getNowDate());
        int cnt = sysAccountConfigMapper.insertSysAccountConfig(sysAccountConfig);
        loadSysAccountConfigCache(sysAccountConfig.getSysAccount());
        return cnt;
    }

    /**
     * 修改配置字典
     *
     * @param sysAccountConfig 配置字典
     * @return 结果
     */
    @Override
    public int updateSysAccountConfig(SysAccountConfig sysAccountConfig) {
        sysAccountConfig.setUpdateTime(DateUtils.getNowDate());
        int cnt = sysAccountConfigMapper.updateSysAccountConfig(sysAccountConfig);
        loadSysAccountConfigCache(sysAccountConfig.getSysAccount());
        return cnt;
    }

    /**
     * 批量删除配置字典
     *
     * @param ids 需要删除的配置字典主键
     * @return 结果
     */
    @Override
    public int deleteSysAccountConfigByIds(String[] ids) {
        return sysAccountConfigMapper.deleteSysAccountConfigByIds(ids);
    }

    /**
     * 删除配置字典信息
     *
     * @param id 配置字典主键
     * @return 结果
     */
    @Override
    public int deleteSysAccountConfigById(String id) {
        return sysAccountConfigMapper.deleteSysAccountConfigById(id);
    }

    /**
     * 加载账套配置缓存数据
     *
     * @param sysAccount
     */
    @Override
    public void loadSysAccountConfigCache(String sysAccount) {
        QueryWrapper<SysAccountConfig> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(sysAccount)) {
            wrapper.eq("sys_account", sysAccount);
        }
        List<SysAccountConfig> configs = sysAccountConfigMapper.selectList(wrapper);
        Map<String, List<SysAccountConfig>> map = configs.stream().collect(Collectors.groupingBy(SysAccountConfig::getSysAccount));
        map.forEach((k, v) -> redisCache.setCacheObject(CacheConstants.SYS_ACCOUNT_CONFIG_KEY + k, JSONObject.toJSONString(v)));
    }
}
